package daoimpl;

import dao.OrderDAO;
import model.Order;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import util.OrderPager;

import java.sql.SQLException;
import java.util.List;

/**
 * Created by admin on 2016/8/15.
 */
public class OrderDAOImpl extends HibernateDaoSupport implements OrderDAO {
    private static final Logger log = LoggerFactory.getLogger(OrderDAOImpl.class);

    public void save(Order order) {
        getHibernateTemplate().save(order);
    }

    public void update(Order order){
        getHibernateTemplate().update(order);
    }

    @Override
    public List findByPage(OrderPager pager) throws Exception {
        List list = null;
        try {
            list = this.getHibernateTemplate().executeFind(new HibernateCallback() {
                @Override
                public Object doInHibernate(Session session) throws HibernateException, SQLException {
                    Query query = session.createQuery("from Order where time >= :startDate and time <= :endDate");
                    query.setParameter("startDate", pager.getStartDate());
                    query.setParameter("endDate", pager.getEndDate());
                    query.setFirstResult( (pager.getCurrentPage()-1) * pager.getPageRow());
                    query.setMaxResults(pager.getPageRow());
                    List data = query.list();
                    return data;
                }
            });
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int getMaxPages(OrderPager pager) {
        int pageRow = pager.getPageRow();
        try {
            List list = this.getHibernateTemplate().executeFind(new HibernateCallback() {
                @Override
                public Object doInHibernate(Session session) throws HibernateException, SQLException {
                    Query query = session.createQuery("select count(*) from Order where time >= :startDate and time <= :endDate");
                    query.setParameter("startDate", pager.getStartDate());
                    query.setParameter("endDate", pager.getEndDate());
                    return query.list();
                }
            });
            int maxRow = (int) list.get(0);
            if (maxRow != 0 && (maxRow % pageRow == 0)){
                return maxRow / pageRow;
            }
            else {
                return (maxRow / pageRow + 1);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    public Order findById(int id) {
        log.debug("getting Order instance with id: " + id);
        try {
            Order instance = (Order) getHibernateTemplate().get("model.Order",
                    id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }

    public void delete(Order order) {
        log.debug("deleting Order instance");
        try {
            getHibernateTemplate().delete(order);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }
}
